#$ARGV[0] is the 0/1 model log file.
#$ARGV[1] is conditional channel.
#$ARGV[2] is number of rounds.
#$ARGV[3] is the numer of integrated rounds.(granulority)

$file = $ARGV[0];

$conditionalChannel = 1;
$totalNum = 100;
$totalRound = 1;

if($ARGV[1]!=0){
	$conditionalChannel = $ARGV[1];
}
if($ARGV[2]!=0){
	$totalNum = $ARGV[2];
}
if($ARGV[3]!=0){
	$totalRound = $ARGV[3];
}

print "totalNum: $totalNum\n";
# if($ARGV[1]!=0){
	# $totalNum = $ARGV[1];
# }
# else {
	# $totalNum = 10000000;
# }

@pathname = split(/\\/,$file);
$len = @pathname;
$path;
for($i=$len-2;$i>=0;$i--){
	$path = "$pathname[$i]\\"."$path";
	print "$path\n";
}

$filename;
@tempfile = split(/\./,$pathname[$len-1]);
$filename = $tempfile[0];
print "file: $filename\n";

$channelNum = 16;
@samples;

@BBCMatric;
@IBCMatric;
@BICMatric;
@IICMatric;

$totalIdle=0;
$totalBusy=0;


for($i=0;$i<$channelNum+1;$i++){
		$BBCMatric[$i]=0;
		$IBCMatric[$i]=0;
		$BICMatric[$i]=0;
		$IICMatric[$i]=0;
}

$alreadyNum = 0;
$startTime = 0;
$previousTime = 0;
$rnd = 1;
open fin, "<$file" or die "can not open file $file!\n";
open fout, ">$path\\condTimeMatric\\CondTimeMatric_$conditionalChannel.dat" or die "can not open file .\\CondTimeMatric_$channel.dat\n";
GETSAMPLE:{
	while (<fin>) {
		chomp;
		@info = split;

		$length = @info;
		if(($length-1)!=$channelNum){
			
			last GETSAMPLE;
		}
		
		if($startTime==0){
			$startTime=$info[0];
			$previousTime = $startTime;
		}
		
		if($info[0]-$previousTime>5000){
			$rnd++;
		}
		
		if($rnd>$totalRound){
			print fout "$startTime ";
			print fout "$totalBusy $totalIdle ";
			print fout "BB ";
			for($i=1;$i<$channelNum+1;$i++){
				if($totalBusy!=0){
					$temp=$BBCMatric[$i]/$totalBusy;
					print fout "$temp ";
				}
				else {
					print fout "NA ";
				}
			}
			print fout "IB ";
			for($i=1;$i<$channelNum+1;$i++){
				if($totalBusy!=0){
					$temp=$IBCMatric[$i]/$totalBusy;
					print fout "$temp ";
				}
				else {
					print fout "NA ";
				}
			}
			print fout "BI ";
			for($i=1;$i<$channelNum+1;$i++){
				if($totalIdle!=0){
					$temp=$BICMatric[$i]/$totalIdle;
					print fout "$temp ";
				}
				else {
					print fout "NA ";
				}
			}
			print fout "II ";
			for($i=1;$i<$channelNum+1;$i++){
				if($totalIdle!=0){
					$temp=$IICMatric[$i]/$totalIdle;
					print fout "$temp ";
				}
				else {
					print fout "NA ";
				}
			}
			print fout "\n";
			$startTime = $info[0];
			for($i=0;$i<$channelNum+1;$i++){
				$BBCMatric[$i]=0;
				$IBCMatric[$i]=0;
				$BICMatric[$i]=0;
				$IICMatric[$i]=0;
			}
			$alreadyNum++;
			$totalBusy=0;
			$totalIdle=0;
			$rnd = 1;
		}
		
		for($i=1;$i<$channelNum+1;$i++){
				if($i==$conditionalChannel && $info[$i]==1){
					$totalBusy++;
				}
				if($i==$conditionalChannel && $info[$i]==0){
					$totalIdle++;
				}
				if($info[$i]==1 && $info[$conditionalChannel]==1){
					$BBCMatric[$i]++;
				}
				if($info[$i]==0 && $info[$conditionalChannel]==1){
					$IBCMatric[$i]++;
				}
				if($info[$i]==1 && $info[$conditionalChannel]==0){
					$BICMatric[$i]++;
				}
				if($info[$i]==0 && $info[$conditionalChannel]==0){
					$IICMatric[$i]++;
				}
		}
		$previousTime = $info[0];
		
		if($alreadyNum==$totalNum){
			last GETSAMPLE;
		}
	}
}
close fin;
close fout;


